# -*- coding: utf-8 -*-
"""
Created on Thu Apr 25 17:21:36 2019

@author:Soly Liang
"""

from numpy import *
import matplotlib.pyplot as plt

# 西瓜3.0α 样本数据
X=array([[0.697,0.46],[0.774,0.376],[0.634,0.264],[0.608,0.318],[0.556,0.215],
   [0.403,0.237],[0.481,0.149],[0.437,0.211],[0.666,0.091],[0.243,0.267],
   [0.245,0.057],[0.343,0.099],[0.639,0.161],[0.657,0.198],[0.36,0.37],
   [0.593,0.042],[0.719,0.103]])
Y=array([1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0])

# LDA
x1=X[Y==1]; x0=X[Y==0]
u1=mean(x1,0); u0=mean(x0,0)
Sw1=dot((x1-u1).T,(x1-u1))/len(x1)
Sw0=dot((x0-u0).T,(x0-u0))/len(x0)
Sw=Sw1+Sw0
w=dot(linalg.inv(Sw),(u0-u1))

#可视化
plt.figure()
kmax=0
for x in x1:   #画正例点及其投影线
    k=dot(w,x)/dot(w,w)
    if abs(k)>abs(kmax):
        kmax=k
    plt.plot([k*w[0],x[0]],[k*w[1],x[1]],'b--')
    plt.scatter(x[0],x[1],c='k',s=30)
    plt.text(x[0],x[1],'+')
for x in x0:   #画反例点及其投影线
    k=dot(w,x)/dot(w,w)
    if abs(k)>abs(kmax):
        kmax=k
    plt.plot([k*w[0],x[0]],[k*w[1],x[1]],'b--')
    plt.scatter(x[0],x[1],c='r',s=30)
    plt.text(x[0],x[1],'-')
kmax*=1.1
plt.plot([0,kmax*w[0]],[0,kmax*w[1]],'b')  #画w直线
plt.text(kmax*w[0],kmax*w[1],'w')
k1=dot(w,u1)/dot(w,w)
k0=dot(w,u0)/dot(w,w)
plt.scatter(k1*w[0],k1*w[1],c='k',s=80)    #画正例投影中点
plt.scatter(k0*w[0],k0*w[1],c='r',s=80)    #画反例投影中点
x=array([0,1])
y=-w[0]/w[1]*(x-(k1+k0)/2*w[0])+(k1+k0)/2*w[1]
plt.plot(x,y,'y')                        #画决策线
plt.text(0.8,0.18,'decision line')

plt.xlim(0)
plt.ylim(0)
plt.xlabel('x1 (Density)')
plt.ylabel('x2 (SugarRatio)')
plt.title('LDA for 3.0α datasets')
plt.show()